package com.weng.coder.service.convertor.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 * @author weng
 */
public class ConvertedFile {

    private String fromPath;
    private String toPath;
    private String fromName;
    private String toName;
    private String fromContent;
    private String toContent;
    private boolean exclude;

    public ConvertedFile() {
    }

    public ConvertedFile(String fromPath, String fromName, String fromContent) {
        this.fromPath = fromPath;
        this.fromName = fromName;
        this.fromContent = fromContent;

        this.toPath = null;
        this.toName = fromName;
        this.toContent = fromContent;
    }

    public boolean isFile() {
        return new File(this.fromPath).isFile();
    }

    public boolean isHidden() {
        return new File(this.fromPath).isHidden();
    }

    private boolean contentChanged() {
        return !this.fromContent.equals(this.toContent);
    }

    public void makeFile() throws IOException {

        if (this.isExclude()) {
            return;
        }

        File file = new File(this.toPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!this.isHidden()) {
            if (this.isFile()) {
                if (this.contentChanged()) {
                    IOUtils.write(this.toContent, new FileOutputStream(file), "UTF8");
                } else {
                    FileUtils.copyFile(new File(this.fromPath), file);
                }
            } else {
                file.mkdir();
            }
        }
    }

    public void replace(String base, String target) {
        this.toContent = this.toContent.replace(base, target);
    }

    public void replacePath(String base, String target) {
        this.toPath = this.toPath == null ? this.fromPath.replace(base, target)
                : this.toPath.replace(base, target);
        this.toName = new File(this.toPath).getName();
    }

    public void replaceName(String base, String target) {
        this.toName = this.toName.replace(base, target);
        this.toPath = new File(this.toPath).getParent() + "/" + this.toName;
    }

    public String getFromPath() {
        return fromPath;
    }

    public void setFromPath(String fromPath) {
        this.fromPath = fromPath;
    }

    public String getToPath() {
        return toPath;
    }

    public void setToPath(String toPath) {
        this.toPath = toPath;
    }

    public String getFromName() {
        return fromName;
    }

    public void setFromName(String fromName) {
        this.fromName = fromName;
    }

    public String getToName() {
        return toName;
    }

    public void setToName(String toName) {
        this.toName = toName;
    }

    public String getFromContent() {
        return fromContent;
    }

    public void setFromContent(String fromContent) {
        this.fromContent = fromContent;
    }

    public String getToContent() {
        return toContent;
    }

    public void setToContent(String toContent) {
        this.toContent = toContent;
    }

    public void setExclude(boolean exclude) {
        this.exclude = exclude;
    }

    public boolean isExclude() {
        return exclude;
    }
}
